var menuManagerForm_page={};

menuManagerForm_page.action = {
    initBody:function () {
        menuManagerForm_page.interface.initForm();
        menuManagerForm_page.action.bindEvent();
    },
    bindEvent:function () {
        // 提交按钮的事件
        layui.use(['form'],function () {
            var form = layui.form;
            form.on('submit(saveBtn)', function (data) {
                menuManagerForm_page.action.saveBtnClick();
                return false;
            });
        });
        // 取消按钮的事件
        $("#cancelBtn").click(function () {
            AppComm.utils.iframeCloseSelf();
            return false;
        });
    },

    // 在做action动作时校验数据data
    checkData:function (action, data) {
        var result = AppComm.data.newResult();
        return result;
    },
    initData:function(rowId){
        menuManagerForm_page.data.rowId = rowId;
    },
    saveBtnClick:function () {
        var params = {
            id:$("#id").val(),
            menuKey:$("#menuKey").val(),
            menuName:$("#menuName").val(),
            parentId:$("#parentId").val(),
            comment:$("#comment").val(),
            target:$("#target").val(),
            orderId:$("#orderId").val(),
            level:$("#level").val(),
            href:$("#href").val()
        };
        var result = menuManagerForm_page.action.checkData(menuManagerForm_page.data.action, params);
        if (result.code==Enums.ResultEnum().FAILURE.code) {
            AppComm.layer.warm(result.msg);
        } else {
            AppComm.ajax.post('/menuManager/ajax/saveMenu',params, function (resultData) {
                parent.AppComm.ajax.onSuccess(resultData);
                AppComm.utils.iframeCloseSelf();
                parent.menuManager_page.action.initBody();
            });
        }
    }
};

menuManagerForm_page.interface = {
    initForm:function () {
        if (menuManagerForm_page.data.rowId == undefined || '' == menuManagerForm_page.data.rowId.trim()) {
            // 添加
            var levelSelect = new CommSelectedSearch("levelDiv", "level", "/menuManager/ajax/getMenuLevelSelect");
            levelSelect.allDataInit();
            var targetSelect = new CommSelectedSearch("targetDiv", "target", "/menuManager/ajax/getMenuTargetSelect");
            targetSelect.allDataInit();
            var parentIdSelect = new CommSelectedSearch("parentIdDiv", "parentId", "/menuManager/ajax/getMenuParentIdDefaultSelect");
            parentIdSelect.allDataInit();

            var html = template("menuManagerForm_template");
            $(".menuManagerForm_box").html(html);
            $("#href_div").hide();
            $("input#href").val("");
            menuManagerForm_page.action.bindEvent();

        } else {
            // 编辑
            AppComm.ajax.post('/menuManager/ajax/getMenuInfoById', {
                id: menuManagerForm_page.data.rowId
            }, function (resultData) {
                var levelSelect = new CommSelectedSearch("levelDiv", "level", "/menuManager/ajax/getMenuLevelSelect");
                levelSelect.allDataInit({level:resultData.data.menuDto.level});
                var targetSelect = new CommSelectedSearch("targetDiv", "target", "/menuManager/ajax/getMenuTargetSelect");
                targetSelect.allDataInit({target:resultData.data.menuDto.target});

                var html = template("menuManagerForm_template", resultData.data);
                $(".menuManagerForm_box").html(html);
                menuManagerForm_page.action.bindEvent();
                if (resultData.data.menuDto.level=="1") {
                    var parentIdSelect = new CommSelectedSearch("parentIdDiv", "parentId", "/menuManager/ajax/getMenuParentIdDefaultSelect");
                    parentIdSelect.allDataInit();
                    $("#href_div").hide();
                } else {
                    var parentIdSelect = new CommSelectedSearch("parentIdDiv", "parentId", "/menuManager/ajax/getMenuParentIdSelect");
                    parentIdSelect.allDataInit({parentId:resultData.data.menuDto.parentId});
                }
            });
        }

        layui.use('form', function() {
            var form = layui.form;
            form.render();
            form.on('select(level)', function (data) {
                if (data.value == "1") {
                    var parentIdSelect = new CommSelectedSearch("parentIdDiv", "parentId", "/menuManager/ajax/getMenuParentIdDefaultSelect");
                    parentIdSelect.allDataInit();
                    $("#href_div").hide();
                    $("input#href").val("");
                } else if (data.value == "2") {

                    var parentIdSelect = new CommSelectedSearch("parentIdDiv", "parentId", "/menuManager/ajax/getMenuParentIdSelect");
                    // var parentIdSelect = new CommSelectedSearch("parentIdDiv", "parentId", "/menuManager/ajax/getMenuLevelSelect");
                    parentIdSelect.allDataInit();
                    $("#href_div").show();
                }
                form.render();
            });
        })

    }
};

menuManagerForm_page.data = {
    'rowId':undefined,
    'dtreeIns':undefined,
};
